package com.hzhr.iot.meterplan.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.meterplan.constant.Meterplan_ErrorsEnum;
import com.hzhr.iot.meterplan.dao.mapper.generator.RuhujihuaEntityMapper;
import com.hzhr.iot.meterplan.dao.mapper.generator.custom.CygMapper;
import com.hzhr.iot.meterplan.dao.mapper.generator.custom.RuhujihuaEntityCustomMapper;
import com.hzhr.iot.meterplan.entity.RuhujihuaEntity;
import com.hzhr.iot.meterplan.entity.ext.RuhujihuaEntityExt;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanGetModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanPutModel;
import com.hzhr.iot.meterplan.model.options.OptionsGetModel;
import com.hzhr.iot.meterplan.model.options.OptionsModel;
import com.hzhr.iot.meterplan.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OptionsService extends BaseService {
    @Autowired
    CygMapper cygMapper;

    //region javadoc注释 : get
    /**
     * 查询选项 <br/><br/>
     * 1. 根据选项类型名称查询选项； <br/>
     * 2. 返回信息； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_typeName : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：选项类型名称;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @throws ServiceException <br/>
     * @see OptionsGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询选项
    //1. 根据选项类型名称查询选项；
    //2. 返回信息；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_typeName", value = "筛选条件：where MeterPlanModel.id = #{equal_id}", required = true, dataType = "string", paramType = "query"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_typeName", value = "筛选条件：选项类型名称", required = true, dataType = "string", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/options", method = RequestMethod.GET)
    //endregion
    public OptionsGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 根据选项类型名称查询选项 */
        String typeName = HzhrTypeUtil.object2string(query.get("_equal_typeName"));
        List<OptionsModel> optionsModels = new ArrayList<>();
        if (typeName.equals("METER_PLAN_TYPE")) {
            List<Map<String, Object>> list = cygMapper.select1();
            if (list != null) {
                for (Map<String, Object> item :
                list) {
                    OptionsModel optionsModel = new OptionsModel();
                    String id =  HzhrTypeUtil.object2string(item.get("ID"));
                    if ("5".equals(id) || "7".equals(id) || "8".equals(id)) {
                        optionsModel.setCode(id);
                        optionsModel.setDes(HzhrTypeUtil.object2string(item.get("DES")));
                        optionsModel.setTypeName("METER_PLAN_TYPE");
                        optionsModels.add(optionsModel);
                    }
                }
            }
        }
        if (typeName.equals("METER_PLAN_STATUS")) {
            List<Map<String, Object>> list = cygMapper.select7();
            if (list != null) {
                for (Map<String, Object> item :
                        list) {
                    OptionsModel optionsModel = new OptionsModel();
                    optionsModel.setCode(HzhrTypeUtil.object2string(item.get("ID")));
                    optionsModel.setDes(HzhrTypeUtil.object2string(item.get("DES")));
                    optionsModel.setTypeName("METER_PLAN_STATUS");
                    optionsModels.add(optionsModel);
                }
            }
        }
        if (typeName.equals("COLLECT_FREQUENCY")) {
            List<Map<String, Object>> list = cygMapper.select2();
            if (list != null) {
                for (Map<String, Object> item :
                        list) {
                    OptionsModel optionsModel = new OptionsModel();
                    optionsModel.setCode(HzhrTypeUtil.object2string(item.get("CF_ID")));
                    optionsModel.setDes(HzhrTypeUtil.object2string(item.get("CF_NAME")));
                    optionsModel.setTypeName("COLLECT_FREQUENCY");
                    optionsModels.add(optionsModel);
                }
            }
        }
        if (typeName.equals("ACCOUNT_PERIOD")) {
            List<Map<String, Object>> list = cygMapper.select3();
            if (list != null) {
                for (Map<String, Object> item :
                        list) {
                    OptionsModel optionsModel = new OptionsModel();
                    optionsModel.setCode(HzhrTypeUtil.object2string(item.get("ID")));
                    optionsModel.setDes(HzhrTypeUtil.object2string(item.get("DES")));
                    optionsModel.setTypeName("ACCOUNT_PERIOD");
                    optionsModels.add(optionsModel);
                }
            }
        }
        if (typeName.equals("PLAN_PERIOD")) {
            List<Map<String, Object>> list = cygMapper.select4();
            if (list != null) {
                for (Map<String, Object> item :
                        list) {
                    OptionsModel optionsModel = new OptionsModel();
                    optionsModel.setCode(HzhrTypeUtil.object2string(item.get("ID")));
                    optionsModel.setDes(HzhrTypeUtil.object2string(item.get("DES")));
                    optionsModel.setTypeName("PLAN_PERIOD");
                    optionsModels.add(optionsModel);
                }
            }
        }
        if (typeName.equals("VEE_CHECK")) {
            List<Map<String, Object>> list = cygMapper.select5();
            if (list != null) {
                for (Map<String, Object> item :
                        list) {
                    OptionsModel optionsModel = new OptionsModel();
                    optionsModel.setCode(HzhrTypeUtil.object2string(item.get("VEE_ID")));
                    optionsModel.setDes(HzhrTypeUtil.object2string(item.get("VEE_NAME")));
                    optionsModel.setTypeName("VEE_CHECK");
                    optionsModels.add(optionsModel);
                }
            }
        }
        if (typeName.equals("METER_USER_TYPE")) {
            List<Map<String, Object>> list = cygMapper.select6();
            if (list != null) {
                for (Map<String, Object> item :
                        list) {
                    OptionsModel optionsModel = new OptionsModel();
                    optionsModel.setCode(HzhrTypeUtil.object2string(item.get("ID")));
                    optionsModel.setDes(HzhrTypeUtil.object2string(item.get("DES")));
                    optionsModel.setTypeName("METER_USER_TYPE");
                    optionsModels.add(optionsModel);
                }
            }
        }

        /* 返回信息 */
        OptionsGetModel result = new OptionsGetModel();
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if ("one".equals(show)) {
            if (optionsModels.size() > 0) {
                result.setModel(optionsModels.get(0));
            }
        }
        if ("list".equals(show)) {
            result.setModelList(optionsModels);
        }
        return result;
    }
}




